home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / terminal / qterm-6.0 / qterm-6 / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-12  |  5.8 KB  |  267 lines

  1. #ifndef lint
  2. static char RCSid[] = 
  3. "$Id: table.c,v 1.7 1993/07/12 20:18:23 mcooper Exp mcooper $";
  4.  
  5. static char copyright[] =
  6. "@(#) Copyright (c) 1990-1993 Michael A. Cooper.\n\
  7.  All rights reserved.\n";
  8. #endif
  9.  
  10. /*
  11.  * Copyright (c) 1990-1993 Michael A. Cooper.
  12.  * This software may be freely distributed provided it is not sold for 
  13.  * profit and the author is credited appropriately.
  14.  */
  15.  
  16. #include "config.h"
  17. #include <stdio.h>
  18. #include <ctype.h>
  19. #include <pwd.h>
  20. #include <sys/param.h>
  21. #include "qterm.h"
  22.  
  23. /*
  24.  * Add termtab (n) entry to main termtab.
  25.  */
  26. int AddTerm(termtable)
  27.     struct termtable            *termtable;
  28. {
  29.     register struct termtable  *tp;
  30.     extern struct termtable    *TermTable;
  31.     
  32.     if (!termtable)
  33.     return(-1);
  34.     
  35.     termtable->nxt = NULL;
  36.     
  37.     if (TermTable == NULL)
  38.     TermTable = termtable;
  39.     else {
  40.     tp = TermTable;
  41.     while(tp && tp->nxt)
  42.       tp = tp->nxt;
  43.     tp->nxt = termtable;
  44.     }
  45.     
  46.     return(0);
  47. }
  48. /*
  49.  * Make a string readable
  50.  */
  51. char *FixCntrl(str, rex)
  52.     char                *str;
  53.     int             rex;
  54. {
  55.     register int         i;
  56.     static char         buf[BUFSIZ];
  57.     
  58.     for (i = 0; str && *str; ) {
  59.     switch (*str) {
  60.     case '\\':
  61.         if (isdigit(*++str)) {
  62.         buf[i] = 0;
  63.         while (isdigit(*str))
  64.             buf[i] = (char) (((int)buf[i] * 8) + 
  65.                      (int)*str++ - (int) '0');
  66.         i++;
  67.         } else
  68.         buf[i++] = *str++;
  69.         continue;
  70.         
  71.     case '^':
  72.         switch (*++str) {
  73.         case '?':
  74.         buf[i++] = '\177';
  75.         break;
  76.         default:
  77.         buf[i++] = *str & 037;
  78.         break;
  79.         }
  80.         break;
  81.         
  82.         /* Special R.E. symbols */
  83.     case '[':
  84.     case '*':
  85.     case '.':
  86.     case '$':
  87.     case '{':
  88.     case '(':
  89.         if (rex)
  90.         buf[i++] = '\\';
  91.         
  92.       default:
  93.         buf[i++] = *str;
  94.     }
  95.     ++str;
  96.     }
  97.     
  98.     buf[i] = (char) NULL;
  99.     
  100.     return(buf);
  101. }
  102.  
  103. /*
  104.  * Make a termtab table
  105.  */
  106. void MakeTable()
  107. {
  108.     char             file[MAXPATHLEN];
  109.     struct passwd            *pwd;
  110.     struct passwd            *getpwuid();
  111.     char                *home;
  112.  
  113.     dprintf("[ initilizing term table... ]\n");
  114.     
  115.     if (TermFile != NULL)
  116.     (void) ReadTabFile(TermFile, FALSE);
  117.  
  118.     if (DoUsrTabFile) {
  119.     /*
  120.      * Try to read the user's own table
  121.      */
  122.     if ((home = getenv("HOME")) == NULL) {
  123.         if ((pwd = getpwuid(getuid())) == NULL) {
  124.         Error("Cannot find password entry for uid %d.", getuid());
  125.         Done(1);
  126.         /*NOTREACHED*/
  127.         }
  128.         home = pwd->pw_dir;
  129.     }
  130.  
  131.     (void) sprintf(file, "%s/%s", home, USRFILE);
  132.     if (ReadTabFile(file, TRUE) < 0) {
  133.         (void) sprintf(file, "%s/%s", home, OLDUSRFILE);
  134.         (void) ReadTabFile(file, TRUE);
  135.     }
  136.     }
  137.     
  138.     if (DoSysTabFile)
  139.     (void) ReadTabFile(TABFILE, FALSE);
  140.     
  141.     dprintf("[ MakeTable done ]\n");
  142. }
  143.  
  144. /*
  145.  * Read a qtermtab file
  146.  */
  147. int ReadTabFile(file, bequiet)
  148.     char                *file;
  149.     int             bequiet;
  150. {
  151.     static char                fields[6][BUFSIZ];
  152.     register int        line = 0;
  153.     static char            buf[BUFSIZ];
  154.     char               *errmsg;
  155.     FILE                *fd;
  156.     struct termtable            *tp;
  157.     int                etype;
  158.     
  159.     if ((fd = fopen(file, "r")) == NULL) {
  160.     if (bequiet) {
  161.         dprintf("[ tab file '%s' can not read: %s ]\n", file, SYSERR);
  162.         return(-1);
  163.     }
  164.     Error("Cannot open file for reading: %s: %s.", file, SYSERR);
  165.     Done(1);
  166.     /*NOTREACHED*/
  167.     }
  168.  
  169.     dprintf("[ Read tab file '%s' ]\n", file);
  170.  
  171.     while (fgets(buf, sizeof(buf), fd)) {
  172.     ++line;
  173.     
  174.     if (buf[0] == '#' || buf[0] == '\n')
  175.         continue;
  176.  
  177.     fields[0][0] = fields[1][0] = fields[2][0] = 
  178.         fields[3][0] = fields[4][0] = fields[5][0] = (char)NULL;
  179.  
  180.     /*
  181.      * Read the first field and use it to figure out what type
  182.      * of entry this is.
  183.      */
  184.     (void) sscanf(buf, "%s", fields[0]);
  185.     if (strncasecmp(buf, K_PRIMARY, 3) == 0)
  186.         etype = ET_PRIMARY;
  187.     else if (strncasecmp(buf, K_SECONDARY, 3) == 0)
  188.         etype = ET_SECONDARY;
  189.     else if (strncasecmp(buf, K_COMSEQ, 3) == 0) {
  190.         extern char          **CommonSeqs;
  191.         register char     **cpp;
  192.         static char          **argv = NULL;
  193.         register int    argc, i;
  194.  
  195.         if ((argc = StrToArgs(buf, &argv, " \t")) > 1) {
  196.         CommonSeqs = (char **) xmalloc(sizeof(char *) * (argc + 1));
  197.         for (cpp = argv + 1, i = 0; cpp && *cpp; ++cpp) {
  198.             CommonSeqs[i++] = StrCopy(*cpp);
  199.             CommonSeqs[i] = (char *)NULL;
  200.         }
  201.         }
  202.         continue;
  203.     } else if (buf[0])    /* Must be an old style entry */
  204.         etype = ET_OLDSTYLE;
  205.     else            /* Must be a blank line */
  206.         continue;
  207.  
  208.     /*
  209.      * Read the whole line based on the entry type
  210.      */
  211.     switch (etype) {
  212.     case ET_PRIMARY:
  213.     case ET_SECONDARY:
  214.         (void) sscanf(buf, "%s%s%s%s%s\t%[^\n]", 
  215.               fields[0], fields[1], fields[2], 
  216.               fields[3], fields[4], fields[5]);
  217.         break;
  218.     case ET_OLDSTYLE:
  219.         (void) sscanf(buf, "%s%s%s\t%[^\n]", 
  220.               fields[1], fields[2], fields[3], fields[5]);
  221.         break;
  222.     }
  223.  
  224.     errmsg = NULL;
  225.     if (fields[1][0] == (char) NULL)
  226.         errmsg = "send string";
  227.     if (fields[2][0] == (char) NULL)
  228.         errmsg = "receive string";
  229.     if (fields[3][0] == (char) NULL)
  230.         errmsg = "generic terminal name";
  231.     if (etype == ET_PRIMARY || etype == ET_SECONDARY)
  232.         if (fields[4][0] == (char) NULL)
  233.         errmsg = "next terminal name";
  234.     if (errmsg) {
  235.         Error("Line %d of %s: Error parsing %s.", line, file, errmsg);
  236.         Done(1);
  237.         /*NOTREACHED*/
  238.     }
  239.  
  240.     tp = (struct termtable *) xmalloc(sizeof(struct termtable));
  241.  
  242.     tp->qt_etype = etype;
  243.     tp->qt_sendstr = StrCopy(FixCntrl((UseAltStr) ? 
  244.                       ALTSEND : fields[1], 0));
  245.     tp->qt_recvstr = StrCopy(FixCntrl(fields[2], 1));
  246.     tp->qt_termname = StrCopy(fields[3]);
  247.     if (fields[4][0])
  248.         tp->qt_ntermname = StrCopy(fields[4]);
  249.     else
  250.         tp->qt_ntermname = tp->qt_termname;
  251.     tp->qt_fullname = StrCopy(fields[5]);
  252.  
  253.     dprintf("\n   Entry Type = %d\n", tp->qt_etype);
  254.     dprintf("  Send String = %s\n", Decode(tp->qt_sendstr));
  255.     dprintf("Expect String = %s\n", Decode(tp->qt_recvstr));
  256.     dprintf("Terminal Name = '%s'\n", tp->qt_termname);
  257.     dprintf("    Next Name = '%s'\n", tp->qt_ntermname);
  258.     dprintf("    Full Name = '%s'\n", 
  259.         (tp->qt_fullname) ? tp->qt_fullname : "<none>");
  260.     
  261.     (void) AddTerm(tp);
  262.     }
  263.     
  264.     return(0);
  265. }
  266.  
  267.